package com.ynwt.service;

import com.github.sd4324530.fastweixin.api.CustomAPI;
import com.ynwt.domain.User;
import com.ynwt.constant.JmsDestinationName;
import com.ynwt.jms.QueueClient;
import com.ynwt.model.BaseRequest;
import com.ynwt.model.ycoin.GetYcoinReponse;
import com.ynwt.repository.UserDao;
import com.ynwt.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jms.JMSException;

/**
 * Created by huangying on 2017/5/4.
 */
@Service
public class SearchServiceImpl implements SearchService{

    public static final Logger LOG = LoggerFactory.getLogger(SearchService.class);

    @Override
    public long getYcoin(String openid) {

        GetYcoinReponse reponse = new GetYcoinReponse();
        BaseRequest request = new BaseRequest();

        User user = userDao.findOne(openid);


        request.setUnionid(user.getUnionid());

        try {
            reponse = JsonUtil.toBean(queueClient.sendAndReceive(request.toString(), JmsDestinationName.GET_YCOIN, 20000), GetYcoinReponse.class);
        } catch (JMSException e) {
            LOG.warn("method: getYcoin. msg: getYcoin jms error.", e);
        }

        if (reponse.isSuccess()) {
            return reponse.getYcoin();
        } else {
            return -1;
        }
    }

    @Override
    public void weekYcoinRank(String unionid) {

    }

    @Autowired
    private QueueClient queueClient;

    @Autowired
    private UserDao userDao;

    @Autowired
    private CustomAPI customAPI;
}
